#include "CTablePinAuth.h"

CTablePinAuth::CTablePinAuth(QObject *parent)
    : AbstractTable{"PinAuth",parent}
{
    QString createSql = "CREATE TABLE PinAuth (  "
        "    UserId          INTEGER PRIMARY KEY"
        "                            UNIQUE"
        "                            NOT NULL,"
        "    PasswdType      INTEGER NOT NULL,"
        "    Passwd          TEXT,"
        "    SaltValue       TEXT,"
        "    TEEHandle       INTEGER UNIQUE,"
        "    FpUid           INTEGER UNIQUE,"
        "    ErrTimes        INTEGER NOT NULL,"
        "    MaxErrTimes     INTEGER NOT NULL,"
        "    FrozenSecond    INTEGER NOT NULL,"
        "    ForzenBeginTime TEXT"
        ");";

    if(!exists()){
        createTable(createSql);
    }
}

CTablePinAuth *CTablePinAuth::getInstance()
{
    static CTablePinAuth *pInstance = nullptr;
    if (nullptr == pInstance) {
        pInstance = new CTablePinAuth();
    }
    return pInstance;
}

bool CTablePinAuth::addPinAuth(const CPinAuthInfo &info)
{
    QVariantMap map;
    map.insert("UserId",info.userId);
    map.insert("PasswdType",info.passwdType);
    map.insert("Passwd",info.passwd);
    map.insert("SaltValue",info.saltValue);
    map.insert("TEEHandle",info.TEEHandle);
    map.insert("FpUid",info.fpUid);
    map.insert("ErrTimes",info.errTimes);
    map.insert("MaxErrTimes",info.maxErrTimes);
    map.insert("ForzenSceond",info.forzenSceond);
    map.insert("FrozenBeginTime",info.frozenBeginTime);

    bool ok = AbstractTable::addRow(map);

    return ok;
}

bool CTablePinAuth::deletePinAuth(const QString &userId)
{
    bool ok = AbstractTable::deleteRow("UserId",userId);
    return ok;
}

bool CTablePinAuth::updatePinAuth(const CPinAuthInfo &info)
{
    QVariantMap map;
    map.insert("PasswdType",info.passwdType);
    map.insert("Passwd",info.passwd);
    map.insert("SaltValue",info.saltValue);
    map.insert("TEEHandle",info.TEEHandle);
    map.insert("FpUid",info.fpUid);
    map.insert("ErrTimes",info.errTimes);
    map.insert("MaxErrTimes",info.maxErrTimes);
    map.insert("ForzenSceond",info.forzenSceond);
    map.insert("FrozenBeginTime",info.frozenBeginTime);

    bool ok = AbstractTable::updateRow("UserId",info.userId,map);

    return ok;
}

QSharedPointer<CPinAuthInfo> CTablePinAuth::pinAuthInfo(const QString &userId)
{
    QSharedPointer<QVariantMap> map = AbstractTable::queryRow("UserId",userId);

    QSharedPointer<CPinAuthInfo> info(new CPinAuthInfo);
    if(map.isNull())
        return info;

    info->userId = userId;
    info->passwdType = map->value("PasswdType").toInt();
    info->passwd = map->value("Passwd").toString();
    info->saltValue = map->value("SaltValue").toString();
    info->TEEHandle = map->value("TEEHandle").toInt();
    info->fpUid = map->value("FpUid").toInt();
    info->errTimes = map->value("ErrTimes").toInt();
    info->maxErrTimes = map->value("MaxErrTimes").toInt();
    info->forzenSceond = map->value("ForzenSceond").toInt();
    info->frozenBeginTime = QDateTime::fromString(map->value("FrozenBeginTime").toString());

    return info;
}
